package com.dldata.devtools30.cfg;

import com.dldata.devtools30.utils.Public;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.*;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.WebApplicationTemplateResolver;
import org.thymeleaf.web.servlet.JakartaServletWebApplication;

import java.util.Arrays;
import java.util.HashSet;


//一定要用WebMvcConfigurer 默认还是 用@EnableAutoConfiguration 继承
// 不建议用WebMvcConfigurationSupport 这个要自定义 一系列的 配置 等同与 @EnableWebMvc
@Configuration
@Slf4j
public class WebSecurityConfigDevtool implements WebMvcConfigurer {


//    @Bean("viewResolverJsp")
//    public InternalResourceViewResolver getViewResolverJsp() {
//        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver("/WEB-INF/jsp/", ".jsp");
//        internalResourceViewResolver.setOrder(10);
//        internalResourceViewResolver.setViewClass(JstlView.class);
//        internalResourceViewResolver.setViewNames("*jsp/**","*cbhs/**","*cwhsconfig/**","*drg/**","*test/**","*main",
//                "*common/**","*System/**","*index/**","*sjcj/**","*hmi/**",
//                "*zkptinter/**","*HBI/GetReport/**","*view/dsnreport/**",
//                "*apimanage/**","*metadata/**","*apimanage/**","*common/**",
//                "*datainterface/**","*index/**","*metadata/**","*PortalView/**","*syscrfform/**",
//                "*PortalView/**","error","*syscrfform/**","*jsqdcrfform/**");
//        return internalResourceViewResolver;
//    }


    @Bean("viewResolverThymeleafDev")
    public org.thymeleaf.spring6.view.ThymeleafViewResolver getViewResolverThymeleaf(ApplicationContext applicationContext) {
        org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver templateResolverError = new org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver();
        templateResolverError.setPrefix("classpath:/templates/thymeleaf/devtools30/");
        templateResolverError.setSuffix(".html");
        templateResolverError.setApplicationContext(applicationContext);
        templateResolverError.setTemplateMode(org.thymeleaf.templatemode.TemplateMode.HTML);
        templateResolverError.setCacheable(false);
        templateResolverError.setResolvablePatterns(new HashSet<>(Arrays.asList(new String[]{"error"})));
        templateResolverError.setOrder(0);
        templateResolverError.setCharacterEncoding("UTF-8");
        
        org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver templateResolverRoot = new org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver();
        templateResolverRoot.setPrefix("classpath:/templates/");
        templateResolverRoot.setSuffix(".html");
        templateResolverRoot.setApplicationContext(applicationContext);
        templateResolverRoot.setTemplateMode(org.thymeleaf.templatemode.TemplateMode.HTML);
        templateResolverRoot.setCacheable(false);
        templateResolverError.setOrder(1);
        templateResolverRoot.setCharacterEncoding("UTF-8");
    
        ServletWebServerApplicationContext context=(ServletWebServerApplicationContext)applicationContext;
        WebApplicationTemplateResolver servletContextTemplateResolver=new WebApplicationTemplateResolver(JakartaServletWebApplication.buildApplication(context.getServletContext()));
        servletContextTemplateResolver.setPrefix("/templates/");
        servletContextTemplateResolver.setSuffix(".html");
        servletContextTemplateResolver.setTemplateMode(org.thymeleaf.templatemode.TemplateMode.HTML);
        servletContextTemplateResolver.setCacheable(false);
        templateResolverError.setOrder(2);
        servletContextTemplateResolver.setCharacterEncoding("UTF-8");
    
        ClassLoaderTemplateResolver classLoaderTemplateResolver=new ClassLoaderTemplateResolver();
        classLoaderTemplateResolver.setPrefix("/templates/");
        classLoaderTemplateResolver.setSuffix(".html");
        classLoaderTemplateResolver.setTemplateMode(org.thymeleaf.templatemode.TemplateMode.HTML);
        classLoaderTemplateResolver.setCacheable(false);
        templateResolverError.setOrder(3);
        classLoaderTemplateResolver.setCharacterEncoding("UTF-8");
    
        org.thymeleaf.spring6.SpringTemplateEngine templateEngine = new org.thymeleaf.spring6.SpringTemplateEngine();
//        templateEngine.addTemplateResolver(servletContextTemplateResolver);
        templateEngine.addTemplateResolver(classLoaderTemplateResolver);
        templateEngine.addTemplateResolver(templateResolverRoot);
        templateEngine.addTemplateResolver(templateResolverError);
        templateEngine.setEnableSpringELCompiler(true);
    
        org.thymeleaf.spring6.view.ThymeleafViewResolver thymeleafViewResolver=new org.thymeleaf.spring6.view.ThymeleafViewResolver();
        thymeleafViewResolver.setTemplateEngine(templateEngine);
        thymeleafViewResolver.setContentType("text/html; charset=utf-8");
        thymeleafViewResolver.setCharacterEncoding("UTF-8");
        thymeleafViewResolver.setViewNames(new String[]{"*thymeleaf/devtools30/**"});
        thymeleafViewResolver.setCache(false);
        thymeleafViewResolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return thymeleafViewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.setOrder(Ordered.LOWEST_PRECEDENCE);
        ResourceHandlerRegistration resourceHandlerRegistration
                = registry.addResourceHandler("/static/devtools30/**");
        resourceHandlerRegistration
                .addResourceLocations(
                        "classpath:/static/devtools30/","/", "classpath:/","classpath:/META-INF/resources/",
                        "classpath:/META-INF/resources/static/devtools30/");
    
        ResourceHandlerRegistration resourceHandlerRegistrationUploadfiles
                = registry.addResourceHandler(Public.image_dir_default+"**");
        String filedir = "file:" + ApplicationHome.getApplicationHome() + Public.webapp_dir_ext + Public.image_dir_default;
        log.info(Public.image_dir_default+"**"+"  "+filedir);
        resourceHandlerRegistrationUploadfiles
                .addResourceLocations(
                        filedir);
    }

}
